package com.md.api;

import com.md.api.common.utils.RedisUtil;
import com.md.api.dao.ShoppingSkuMapper;
import com.md.api.entity.ShoppingSku;
import lombok.extern.slf4j.Slf4j;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.context.annotation.ComponentScan;

import javax.annotation.Resource;
import java.util.List;
@Slf4j
@EnableDiscoveryClient
@SpringBootApplication
@ComponentScan(basePackages = {"com.md.api",  "com.md.api.common.interceptor", "com.md.api.common.utils" ,"com.md.api.dao"})
@MapperScan( basePackages={"com.md.api.dao"})
public class ApiApplication implements CommandLineRunner {

    public static void main(String[] args) {
        SpringApplicationBuilder builder = new SpringApplicationBuilder(ApiApplication.class);
        builder.headless(false).run(args);
    }

    @Override
    public void run(String... args) throws Exception {
        initShoppingData();
    }

    @Autowired
    private RedisUtil redisUtil;
    @Resource
    private ShoppingSkuMapper shoppingSkuMapper;



    private void  initShoppingData(){
        List<ShoppingSku> skus = shoppingSkuMapper.initRedisCount();
        for (ShoppingSku sku:skus){
            if (null!=sku.getStockCount()){
                String key = "sku_" + sku.getSkuId();
                redisUtil.set(key, sku.getStockCount());
            }
        }
        log.info("初始化商城产品数量成功！");
    }

}
